<?php
require("./common.php");
include S_ROOT."include/image.class.php";
include(S_ROOT."data/dataconfig.php");
if(empty($_GET['small'])){
	if(empty($_POST['step'])){
		/**
		 * 作品列表
		 **/
		include(S_ROOT."data/dataconfig.php");
		$page = empty($_GET['page']) ? 1 : $_GET['page'];
		$pageSize = empty($_CONFIG['imageNum']) ? 5 : $_CONFIG['imageNum'];
		$order  = empty($_GET['order']) ? "createtime" : $_GET['order'];

		$limit = " LIMIT ".($page-1)*$pageSize.",".$pageSize;
		$query = $db->query("SELECT imageurl,id FROM ".tname('image')."  WHERE ifdelete=0  ORDER BY $order DESC $limit");
		$list = $db->fetch_all($query);
		$count = $db->get_one("SELECT count(*) as amount FROM ".tname('image')."  WHERE ifdelete=0  ORDER BY $order DESC  ");

		$pagenum = ceil($count['amount']/$pageSize);

		include template("post");
	}else{
		$data['title'] = trim($_POST['title']);
		if(empty($data['title'])){
			showmessage("作品标题不能为空！");
		}
		$data['author'] = trim($_POST['author']);
		if(empty($data['author'])){
			showmessage("作者不能为空！");
		}
		$data['content'] = $_POST['content'];
		if(empty($data['content'])){
		//	showmessage("作品内容不能为空！");
		}
		$data['phone'] = $_POST['phone'];
		if(empty($data['phone'])){
			showmessage("作者联系电话不能为空！");
		}

		$data['imageurl'] = $_POST['imageurl'];
		if(empty($data['imageurl'])){
			showmessage("上传作品不能为空！");
		}

		$data['thumburl'] = $_POST['thumburl'];
		if(empty($data['thumburl'])){
			showmessage("上传作品缩略图不能为空！");
		}

		$data['description'] = $_POST['description'];
		
//		$fileurl = $_FILES['imageurl'];
//
//		if (!$fileurl['name']) {
//			$fileurl = array();
//		}
//		if (empty($fileurl['name'])) {
//			showmessage('请上传图片');
//		}
//		if (!empty($fileurl)) {
//			if ($fileurl['error'] !== 0) {
//				showmessage('上传图片发生错误');
//			}else {
//				$extname = fileext($fileurl['name']);
//				$allow = array('jpg','jpeg','gif','png','tif','bmp');
//				if(!in_array($extname,$allow)) {
//					showmessage('请上传以'.join(',',$allow)."结尾的图片！");
//				}
//				$fileName  = 'upload/img/'.md5(time());
//				$uploadurl = $fileName.'.'.$extname;
//				$thumbUrl  = $fileName.'_thumb.'.$extname;
//				move_uploaded_file($fileurl['tmp_name'],S_ROOT.$uploadurl);
//				$tp = new image();
//				$tp->thumb(S_ROOT.$uploadurl,S_ROOT.$thumbUrl,$_CONFIG['thumbwidth'],$_CONFIG['thumbheight']);
//				$data['imageurl'] =  $uploadurl;
//				$data['thumburl'] =  $thumbUrl;
//			}
//		}
		
		$data['createtime'] = time();
		
		inserttable('image',$data);

		$id = $db->insert_id();
		showmessage('作品上传成功！',"photo.php?id=".$id);
//		header("content-type:text/html; charset=utf-8");
//		echo "<script language='javascript'>
//				alert('作品上传成功');
//				parent.document.location.href='photo.php?id=$id';
//				parent.tb_remove();
//			  </script>
//		 ";
	}
}else{
	if(empty($_POST['step'])){

		include template("postsmall");
	}else{
		$data['title'] = trim($_POST['title']);
		if(empty($data['title'])){
			showmessage("作品标题不能为空！");
		}
		$data['author'] = trim($_POST['author']);
		if(empty($data['author'])){
			showmessage("作者不能为空！");
		}
		$data['content'] = $_POST['content'];
		if(empty($data['content'])){
		//	showmessage("作品内容不能为空！");
		}
		$data['phone'] = $_POST['phone'];
		if(empty($data['phone'])){
			showmessage("作者联系电话不能为空！");
		}

		$data['imageurl'] = $_POST['imageurl'];
		if(empty($data['imageurl'])){
			showmessage("上传作品不能为空！");
		}

		$data['thumburl'] = $_POST['thumburl'];
		if(empty($data['thumburl'])){
			showmessage("上传作品缩略图不能为空！");
		}
		$data['description'] = $_POST['description'];
		
		$fileurl = $_FILES['imageurl'];

		if (!$fileurl['name']) {
			$fileurl = array();
		}
		
//		/**
//		 * 限制文件大小 1M以内
//		 **/
//		if($fileurl['size']/1024/1024>1){
//			showmessage('图片大小不能超过1M！');
//		}
//
//		if (empty($fileurl['name'])) {
//			showmessage('请上传图片');
//		}
//		if (!empty($fileurl)) {
//			if ($fileurl['error'] !== 0) {
//				showmessage('上传图片发生错误');
//			}else {
//				$extname = fileext($fileurl['name']);
//				$allow = array('jpg','jpeg','gif','png','tif','bmp');
//				if(!in_array($extname,$allow)) {
//					showmessage('请上传以'.join(',',$allow)."结尾的图片！");
//				}
//				$fileName  = 'upload/img/'.md5(time());
//				$uploadurl = $fileName.'.'.$extname;
//				$thumbUrl  = $fileName.'_thumb.'.$extname;
//				move_uploaded_file($fileurl['tmp_name'],S_ROOT.$uploadurl);
//				$tp = new image();
//				$tp->thumb(S_ROOT.$uploadurl,S_ROOT.$thumbUrl,$_CONFIG['thumbwidth'],$_CONFIG['thumbheight']);
//				$data['imageurl'] =  $uploadurl;
//				$data['thumburl'] =  $thumbUrl;
//			}
//		}
//		
		$data['createtime'] = time();
		
		inserttable('image',$data);

		$id = $db->insert_id();

		header("content-type:text/html; charset=utf-8");
		echo "<script language='javascript'>
				alert('作品发布成功');
				parent.document.location.href='photo.php?id=$id';
				parent.tb_remove();
			  </script>
		 ";
	}
}
?>